package com.cash.channel.svc.service.common;

import com.alibaba.fastjson.JSON;
import com.cash.channel.api.constant.Constant;
import com.cash.channel.api.exception.CashChannelException;
import com.cash.channel.api.model.ApiRequestModel;
import com.cash.channel.api.model.ApiResponseModel;
import com.cash.channel.api.model.request.RepayPlanListRequest;
import com.cash.channel.api.model.request.UserVerifyRequest;
import com.cash.channel.api.model.response.UserVerifyResponse;
import com.cash.channel.api.model.user.ThirdIdModel;
import com.cash.channel.svc.logic.ImageLogic;
import com.cash.channel.svc.logic.OrderLogic;
import com.cash.channel.svc.logic.ThirdCommonLogic;
import com.cash.channel.svc.service.base.UserVerify;
import com.cash.user.model.UserInfoResultModel;
import com.cash.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service(Constant.CHANNEL.COMMON+ Constant.SERVICE.活体校验)
public class CommonUserVerify implements UserVerify {

    @Autowired
    private ThirdCommonLogic thirdCommonLogic;

    @Autowired
    private OrderLogic orderLogic;

    @Autowired
    private UserService userService;

    @Autowired
    private ImageLogic imageLogic;

    @Override
    public ApiResponseModel<UserVerifyResponse> doUserVerify(ApiRequestModel apiRequestModel) {
        try {
            UserVerifyRequest userVerifyRequest = JSON.parseObject(apiRequestModel.getData(), UserVerifyRequest.class);
            ThirdIdModel thirdIdModel = thirdCommonLogic.findThirdIdModelByThirdOrderId(userVerifyRequest.getOrderNo(), apiRequestModel.getChannel());
            Map<String, Object> map = orderLogic.getWDBYBAAPI(userVerifyRequest.getOrderNo());
            log.info("更新提现订单号"+userVerifyRequest.getOrderNo()+ MapUtils.getString(map, "wdId"));
            thirdIdModel.setCWdId(MapUtils.getString(map, "wdId"));
            thirdCommonLogic.insertOrUpdateIdMapping(thirdIdModel.getCThirdId(), apiRequestModel.getChannel(), thirdIdModel);
            String imageUrl1 = imageLogic.uploadBase64Image(userVerifyRequest.getImage1());
            String imageUrl2 = null;
            String imageUrl3 = null;
            String imageUrl4 = null;
            try {
                imageUrl2 = imageLogic.uploadBase64Image(userVerifyRequest.getImage2());
                imageUrl3 = imageLogic.uploadBase64Image(userVerifyRequest.getImage3());
                imageUrl4 = imageLogic.uploadBase64Image(userVerifyRequest.getImage4());
            } catch (Exception e) {
                log.error(e.getMessage());
            }
            Map<String, Object> verifyMap = userService.verifyFace(MapUtils.getString(map, "wdId"), thirdIdModel.getCAppId(),
                    thirdIdModel.getCUserUid(), imageUrl1, imageUrl2, imageUrl3, imageUrl4);
            log.info("r360 人脸识别并提交提现订单-进行人脸识别结束：" + verifyMap);
            UserVerifyResponse response =new UserVerifyResponse();
            response.setFaceFlowId(MapUtils.getString(verifyMap, "faceFlowId"));
            return new ApiResponseModel<>(true, 0, "", response);
        } catch (CashChannelException e) {
            log.error(e.getErrMessage(), e);
            return new ApiResponseModel<>(false, -1, e.getErrMessage(), null);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new ApiResponseModel<>(false, -1, e.getMessage(), null);
        }
    }
}
